package com.guigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.MenusMapper;
import com.guigu.pojo.Menus;
import com.guigu.pojo.Roles;
import com.guigu.pojo.SystemUsers;
import com.guigu.pojo.Users;
import com.guigu.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {


    @Autowired
    MenusMapper menusMapper;

    //显示用户权限数据
    @Override
    public List<Menus> show(Users users) {
        Integer parentId = 0;
        List<Menus> menus = menusMapper.show(users,parentId);
        for (Menus menus02 : menus) {
            System.out.println("菜单"+menus02.getMName());
            List<Menus> menusList=menusMapper.show(users,menus02.getId());
            for (Menus menus03 : menusList){
                menus03.setChildMenus(menusMapper.show(users,menus03.getId()));
            }
            menus02.setChildMenus(menusList);
        }
        return menus;
    }

    //后端登录权限数据显示
    public List<Menus> systemShow(SystemUsers systemUsers) {
        Integer parentId = 0;
        List<Menus> menus = menusMapper.systemShow(systemUsers,parentId);
            System.out.println("一及菜单"+menus);
        for (Menus menus02 : menus) {
            System.out.println("二及菜单"+menus02);
            List<Menus> menusList=menusMapper.systemShow(systemUsers,menus02.getId());
            for (Menus menus03 : menusList){
                System.out.println("三及菜单"+menus03);
                menus03.setChildMenus(menusMapper.systemShow(systemUsers,menus03.getId()));
            }
            menus02.setChildMenus(menusList);
        }
        return menus;
    }


    //查找各个角色的权限
    @Override
    public List<Menus> querySystembyrid(Roles roles) {


        return menusMapper.querySystembyrid(roles);
    }

    //后端面包屑导航
    @Override
    public List<Menus> quertop(Integer mid,SystemUsers systemUsers) {
        Integer parentId = 0;
        List<Menus> menus = menusMapper.quertop(mid,parentId);
        for (Menus menus02 : menus) {
            List<Menus> menusList=menusMapper.systemShow(systemUsers,menus02.getId());
            for (Menus menus03 : menusList){
                menus03.setChildMenus(menusMapper.systemShow(systemUsers,menus03.getId()));
            }
            menus02.setChildMenus(menusList);
        }
        return menus;
    }


    @Override
    public List<Menus> showmenusall() {
        Integer parentId = 0;
        List<Menus> menus = menusMapper.showmenusall(parentId);
        for (Menus menus02 : menus) {
            List<Menus> menusList=menusMapper.showmenusall(menus02.getId());
            for (Menus menus03 : menusList){
                menus03.setChildMenus(menusMapper.showmenusall(menus03.getId()));
            }
            menus02.setChildMenus(menusList);
        }
        return menus;
    }

    //前端面包屑
    @Override
    public List<Menus> querusertop(Integer mid, Users users) {
        Integer parentId = 0;
        List<Menus> menus = menusMapper.queractive(mid,parentId);
        for (Menus menus02 : menus) {
            System.out.println("菜单"+menus02.getMName());
            List<Menus> menusList=menusMapper.show(users,menus02.getId());
            for (Menus menus03 : menusList){
                menus03.setChildMenus(menusMapper.show(users,menus03.getId()));
            }
            menus02.setChildMenus(menusList);
        }
        return menus;
    }

    //分页显示所有菜单
    @Override
    public Page<Menus> querallmenus(Integer grade,Menus menus, Integer pagon, Integer pagesize) {

        QueryWrapper<Menus> queryWrapper
                = new QueryWrapper<>();

        if (!StringUtils.isEmpty(menus.getMName())){
            queryWrapper.like("m_name",menus.getMName());
        }

        Page<Menus> page =new Page<>(pagon,pagesize);
        page.setPages(pagon);
        page.setSize(pagesize);



        return menusMapper.selectPage(page,queryWrapper);
    }

    //添加菜单
    @Override
    public Map addmenus(Menus menus,Integer menusgrade) {
        Map map = new HashMap();
        map.put("msg","添加失败");

        if (menusgrade==1){
            System.out.println("一级菜单");
            menus.setParentId(0);
            if (menusMapper.insert(menus)>0){
                map.put("msg","添加成功");
            }
        }else if (menusgrade==2){
            System.out.println("二级菜单");
            if (menusMapper.insert(menus)>0){
                map.put("msg","添加成功");
            }
        }else if (menusgrade==3){
            System.out.println("二级菜单");
            if (menusMapper.insert(menus)>0){
                map.put("msg","添加成功");
            }
        }

        return map;
    }

    //查找所有的一级菜单
    @Override
    public List<Menus> querallfirstmenus() {

        QueryWrapper<Menus> queryWrapper
                = new QueryWrapper<>();

        queryWrapper.eq("parent_id",0);

        return menusMapper.selectList(queryWrapper);
    }

    //查找所有的二级菜单
    @Override
    public List<Menus> querallsecondmenus() {
        List<Menus> list1 = new ArrayList();

        QueryWrapper<Menus> queryWrapper01
                = new QueryWrapper<>();
        queryWrapper01.eq("parent_id",0);

        List<Menus> list = menusMapper.selectList(queryWrapper01);

        QueryWrapper<Menus> queryWrapper
                = new QueryWrapper();


        for (Menus menus : list) {
            System.out.println("菜单id"+menus.getId());
            List list2 = new ArrayList();
            list2=menusMapper.querallsecondmenus(menus.getId());
            list1.addAll(list2);
        }

        return list1;
    }

    //修改状态
    @Override
    public Map altermenusbyid(Menus menus) {
        Map map =new HashMap();
        map.put("msg","修改失败");

        if (menus.getState()==1){
            menus.setState(0);
            if (menusMapper.updateById(menus)>0){
                map.put("msg","修改成功");
            }
        }else{
            menus.setState(1);
            if (menusMapper.updateById(menus)>0){
                map.put("msg","修改成功");
            }
        }


        return map;
    }

    //批量修改
    @Override
    public Map alterallmenus(Menus menus,Integer[] id) {
        Map map = new HashMap();
        map.put("msg","修改失败");

        if (menus.getState()==0){
            for (Integer integer : id) {
                menus.setId(integer);
                menus.setState(1);
                if (menusMapper.updateById(menus)>0){
                    map.put("msg","修改成功");
                }
            }
        }else {
            for (Integer integer : id) {
                menus.setId(integer);
                menus.setState(0);
                if (menusMapper.updateById(menus)>0){
                    map.put("msg","修改成功");
                }
            }
        }


        return map;
    }
}
